package cn.com.surker.sale.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.sale.dao.SaleContractDao;
import cn.com.surker.sale.entity.SaleContract;
import cn.com.surker.sale.entity.SaleContractSubsidy;
import cn.com.surker.sale.dao.SaleContractSubsidyDao;
import cn.com.surker.sale.service.SaleContractSubsidyBaseService;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.*;

/**
* <p>
    * 销售合同补贴表 服务实现类
    * </p>
*
* @author admin
* @since 2024-07-09
*/
public abstract class SaleContractSubsidyBaseServiceImpl extends ServiceImpl<SaleContractSubsidyDao, SaleContractSubsidy> implements SaleContractSubsidyBaseService {

    @Resource
    protected SaleContractSubsidyDao saleContractSubsidyDao;
    @Resource
    protected SaleContractDao saleContractDao;

    /**
     * 查询销售合同砼单价信息
     * @param contractId
     * @return
     */
    public Map<String, List<SaleContractSubsidy>> findByContract(Long contractId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<SaleContractSubsidy> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        query.orderByDesc("take_time");
        List<SaleContractSubsidy> priceList = saleContractSubsidyDao.selectList(query);
        if (priceList == null || priceList.size() == 0)
            return null;
        Map<String, List<SaleContractSubsidy>> map = new LinkedHashMap<>();
        for (int i = 0; i < priceList.size(); i++) {
            SaleContractSubsidy subsidy = priceList.get(i);
            String takeTime = DateUtil.convertString(subsidy.getTakeTime());
            if (map.containsKey(takeTime)) {
                List<SaleContractSubsidy> tempList = map.get(takeTime);
                tempList.add(subsidy);
                map.put(takeTime, tempList);
            } else {
                List<SaleContractSubsidy> tempList = new ArrayList<>();
                tempList.add(subsidy);
                map.put(takeTime, tempList);
            }
        }
        return map;
    }

    /**
     * 新增补贴价信息
     * @param orgId
     * @param contractId 合同ID
     * @param subsidyType 补贴方式(0:泵送方量1:运输方量,2:超时浇筑,3:润管剂/:润泵剂,4:接管补助,5：砂浆)
     * @param price 单价
     * @param takeTime 生效时间
     * @param countStere 计算方量
     * @param minStere 最小方量
     * @param num 次数
     * @param remark
     * @param creator
     * @param additionalCountType 附加补贴计算方式
     * @return
     */
    public Integer saveSubsidy(Long orgId, Long contractId, Integer subsidyType,Integer countType, Double price, Double taxPrice,
                        String takeTime, String remark, Double minStere, Double countStere, Integer num,
                        Long pumpNormId,Integer additionalCountType, String creator, String expireTime) {
        if (StringUtils.isAllParamNull(orgId, contractId, subsidyType, countType, price, takeTime))
            return Code.PARAM_EMPTY.getCode();
        if (price < 1)
            return Code.PRICE_LT1.getCode();
        if (StringUtils.isNull(num))
            num = 1;
        Date nowDate = new Date();
        taxPrice = price;
        Date takeDate = DateUtil.convertDate(takeTime);

        if (StringUtils.isNull(subsidyType))
            return Code.CONTRACT_SUBSIDY_COUNT_EMPTY.getCode();
        if (StringUtils.isNull(countType))
            return Code.CONTRACT_SUBSIDY_TYPE_EMPTY.getCode();
        SaleContract saleContract = saleContractDao.selectById(contractId);
        if (saleContract == null)
            return Code.CONTRACT_EMPTY.getCode();
        if (StringUtils.isNull(pumpNormId))
            pumpNormId = -1l;
        if (StringUtils.isNull(additionalCountType))
            additionalCountType = -1;
        SaleContractSubsidy scs = saleContractSubsidyDao.findTakeTime(orgId, contractId, subsidyType, countType, pumpNormId, additionalCountType, takeDate);
        if (scs != null)
            return Code.EXE_CONTRACT_SUBSIDY.getCode();

        SaleContractSubsidy subsidy = new SaleContractSubsidy(orgId, contractId, subsidyType,countType, minStere, countStere, num,
                price, 0, takeDate,  remark, creator, nowDate);
        subsidy.setTaxPrice(taxPrice);
        subsidy.setPumpNormId(pumpNormId);
        subsidy.setAdditionalCountType(additionalCountType);
        if (!StringUtils.isNull(expireTime)) {
            subsidy.setExpireTime(DateUtil.convertDate(expireTime));
        }
        Integer code = saleContractSubsidyDao.insert(subsidy);
        if (code > 0)
            return 0;

        return -1;
    }
    /**
     *
     * @param id
     * @param contractId 合同ID
     * @param subsidyType 补贴方式(0:泵送方量1:运输方量,2:超时浇筑,3:润管剂/:润泵剂,4:接管补助,5：砂浆)
     * @param price 单价
     * @param takeTime 生效时间
     * @param countStere 计算方量
     * @param minStere 最小方量
     * @param num 次数
     * @param remark
     * @param creator
     * @return
     */
    public Integer updateSubsidy(Long id, Long contractId, Integer subsidyType,Integer countType, Double price, Double taxPrice,
                          String takeTime, String remark, Double minStere, Double countStere, Integer num,
                          Long pumpNormId,Integer additionalCountType, String creator, String expireTime) {

        if (StringUtils.isNull(id))
            return Code.PARAM_EMPTY.getCode();
        SaleContractSubsidy contractSubsidy = saleContractSubsidyDao.selectById(id);
        Date nowDate = new Date();
        if (contractSubsidy == null || contractSubsidy.getStatus() == 1)
            return Code.ERROR_DATA.getCode();
        taxPrice = price;
        if (StringUtils.isNull(pumpNormId))
            pumpNormId = -1l;
        if (!StringUtils.isNull(price) && price > 0)
            contractSubsidy.setPrice(price);
        if (!StringUtils.isNull(taxPrice) && taxPrice > 0)
            contractSubsidy.setTaxPrice(taxPrice);
        if (!StringUtils.isNull(takeTime)) {
            Date takeDate = DateUtil.convertDate(takeTime);
            contractSubsidy.setTakeTime(takeDate);
        }
        if (!StringUtils.isNull(subsidyType))
            contractSubsidy.setSubsidyType(subsidyType);
        if (!StringUtils.isNull(countType))
            contractSubsidy.setCountType(countType);
        if (!StringUtils.isNull(minStere))
            contractSubsidy.setMinStere(minStere);
        if (!StringUtils.isNull(countStere))
            contractSubsidy.setCountStere(countStere);
        if (!StringUtils.isNull(num))
            contractSubsidy.setNum(num);
        if (!StringUtils.isNull(remark))
            contractSubsidy.setRemark(remark);
        if (StringUtils.isNull(pumpNormId))
            pumpNormId = -1l;
        if (StringUtils.isNull(additionalCountType))
            additionalCountType = -1;
        if (!StringUtils.isNull(expireTime)) {
            contractSubsidy.setExpireTime(DateUtil.convertDate(expireTime));
        }
        contractSubsidy.setPumpNormId(pumpNormId);
        contractSubsidy.setAdditionalCountType(additionalCountType);

//        SaleContractSubsidy scs = saleContractSubsidyDao.findTakeTime(contractSubsidy.getOrgId(), contractId,
//                subsidyType, countType, pumpNormId, contractSubsidy.getTakeTime());
//        if (scs != null)
//            return Code.EXE_CONTRACT_SUBSIDY.getCode();
        contractSubsidy.setUpdateTime(nowDate);
        contractSubsidy.setUpdator(creator);
        Integer code = saleContractSubsidyDao.updateById(contractSubsidy);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 禁用合同
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isNull(id))
            return Code.PARAM_EMPTY.getCode();
        SaleContractSubsidy contractSubsidy = saleContractSubsidyDao.selectById(id);
        Date nowDate = new Date();
        if (contractSubsidy == null)
            return Code.ERROR_DATA.getCode();
        contractSubsidy.setStatus(status);
        contractSubsidy.setUpdateTime(nowDate);
        contractSubsidy.setUpdator(creator);
        Integer code = saleContractSubsidyDao.updateById(contractSubsidy);
        if (code > 0)
            return 0;
        return -1;
    }
    /**
     * 批量新增合同补贴信息
     * @param subsidyList
     * @return
     */
    public Integer saveBatchSubsidy(List<SaleContractSubsidy> subsidyList, Long contractId, String creator) {
        if (subsidyList == null || subsidyList.size() == 0)
            return 0;
        List<SaleContractSubsidy> tempList = new ArrayList<>();
        Date nowDate = new Date();
        for (int i = 0; i < subsidyList.size(); i++) {
            SaleContractSubsidy subsidy = subsidyList.get(i);
            subsidy.setContractId(contractId);
            Integer status = subsidy.getStatus();
            if (StringUtils.isNull(subsidy.getSubsidyType()))
                continue;
            if (StringUtils.isNull(subsidy.getCountType()))
                continue;
            if (StringUtils.isNull(status))
                subsidy.setStatus(0);
            if (!StringUtils.isNull(subsidy.getId())) {
                subsidy.setCreateTime(nowDate);
                subsidy.setCreator(creator);
            } else {
                subsidy.setUpdateTime(nowDate);
                subsidy.setUpdator(creator);
            }
            tempList.add(subsidy);
        }
        Integer code = saleContractSubsidyDao.saveBatch(subsidyList);
        if (code > 0)
            return 0;
        return -1;
    }

	@Override
	public boolean hasSubsidy(Long conId) {
		if (conId == null) {
			return false;
		}
		QueryWrapper<SaleContractSubsidy> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("contract_id", conId);
		Integer integer = saleContractSubsidyDao.selectCount(qw);
		if (integer != null && integer > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<SaleContractSubsidy> findSubsidy(Long orgId, Long conId, String takeTime) {
		if (orgId == null || conId == null || StringUtils.isBlank(takeTime)) {
			return null;
		}
		List<SaleContractSubsidy> list = saleContractSubsidyDao.findSubsidy(orgId, conId, takeTime);
		return list;
	}
}